package com.atkid.boot.service.impl;

import com.atkid.boot.entity.Club;
import com.atkid.boot.entity.Student;
import com.atkid.boot.entity.StudentInfo;
import com.atkid.boot.entity.vo.StuVo;
import com.atkid.boot.entity.vo.StudentVo;
import com.atkid.boot.mapper.StudentMapper;
import com.atkid.boot.service.ClubService;
import com.atkid.boot.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 * @author kid
 * @since 2021-12-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements
		StudentService {

	@Autowired
	ClubService clubService;

	@Override
	public Page<Student> getAllStudent(Long current, Long limit) {
		Page<Student> page = new Page<>(current, limit);
		QueryWrapper<Student> wrapper = new QueryWrapper<>();
		wrapper.isNotNull("club1_id").or().isNotNull("club2_id");
		this.page(page, wrapper);
		return page;
	}

	@Override
	public List<StuVo> getByClub(int cid) {
		QueryWrapper<Student> qw = new QueryWrapper<>();
		qw.eq("club1_id", cid).or().eq("club2_id", cid);
		List<Student> studentList = this.list(qw);
		List<StuVo> res = new ArrayList<>();
		for (Student student : studentList) {
			StuVo tmp = new StuVo();
			tmp.setName(student.getName());
			tmp.setClassName(student.getClassName());
			tmp.setDepartment(student.getDepartment());
			tmp.setId(student.getId());
			tmp.setPhone(student.getPhone());
			Integer club1Id = student.getClub1Id();
			Integer club2Id = student.getClub2Id();
			if (club1Id != null && club1Id == cid) {
				Club club = clubService.getById(cid);
				tmp.setClub(club.getName());
				tmp.setTime(student.getTime1());
			} else if (club2Id != null && club2Id == cid) {
				Club club = clubService.getById(cid);
				tmp.setClub(club.getName());
				tmp.setTime(student.getTime2());
			}
			if (student.getSex() == 1) {
				tmp.setSex("男");
			} else if (student.getSex() == 0) {
				tmp.setSex("女");
			}
			res.add(tmp);
		}
		return res;
	}

	@Override
	public Page<StuVo> getByClub(int cid, Long current, Long size) {
		Page<Student> studentPage = new Page<>(current, size);
		QueryWrapper<Student> qw = new QueryWrapper<>();
		qw.eq("club1_id", cid).or().eq("club2_id", cid);
		this.page(studentPage, qw);
		List<Student> studentList = studentPage.getRecords();
		List<StuVo> res = new ArrayList<>();
		for (Student student : studentList) {
			StuVo tmp = new StuVo();
			tmp.setName(student.getName());
			tmp.setClassName(student.getClassName());
			tmp.setDepartment(student.getDepartment());
			tmp.setId(student.getId());
			Integer club1Id = student.getClub1Id();
			Integer club2Id = student.getClub2Id();
			if (club1Id != null && club1Id == cid) {
				Club club = clubService.getById(cid);
				tmp.setClub(club.getName());
				tmp.setTime(student.getTime1());
			} else if (club2Id != null && club2Id == cid) {
				Club club = clubService.getById(cid);
				tmp.setClub(club.getName());
				tmp.setTime(student.getTime2());
			}
			if (student.getSex() == 1) {
				tmp.setSex("男");
			} else if (student.getSex() == 0) {
				tmp.setSex("女");
			}
			res.add(tmp);
		}
		Page<StuVo> stuVoPage = new Page<>(current, size);
		stuVoPage.setPages(stuVoPage.getPages());
		stuVoPage.setRecords(res);
		stuVoPage.setCurrent(current);
		stuVoPage.setSize(size);
		stuVoPage.setTotal(studentPage.getTotal());
		return stuVoPage;
	}

	@Override
	public Page<StudentVo> transferStu(Page<Student> stuPage) {
		List<Student> studentList = stuPage.getRecords();
		List<StudentVo> stuVos = new ArrayList<>();
		Page<StudentVo> pageVo = new Page<>();
		for (Student student : studentList) {
			StudentVo tmp = new StudentVo();
			tmp.setId(student.getId());
			tmp.setName(student.getName());
			tmp.setClassName(student.getClassName());
			tmp.setDepartment(student.getDepartment());
			tmp.setPhone(student.getPhone());
			Integer cid_1 = student.getClub1Id();
			if (cid_1 != null) {
				Club club1 = clubService.getById(cid_1);
				tmp.setClub1(club1.getName());
			} else {
				tmp.setClub1(null);
			}

			Integer cid_2 = student.getClub2Id();
			if (cid_2 != null) {
				Club club2 = clubService.getById(cid_2);
				tmp.setClub2(club2.getName());
			} else {
				tmp.setClub2(null);
			}
			tmp.setTime1(student.getTime1());
			tmp.setTime2(student.getTime2());
			if (student.getSex() == 1) {
				tmp.setSex("男");
			} else if (student.getSex() == 0) {
				tmp.setSex("女");
			}
			stuVos.add(tmp);
		}
		pageVo.setRecords(stuVos);
		pageVo.setTotal(stuPage.getTotal());
		pageVo.setCurrent(stuPage.getCurrent());
		pageVo.setSize(stuPage.getSize());
		pageVo.setPages(stuPage.getPages());
		return pageVo;
	}

	@Override
	public StudentVo getVoByStudent(Student student) {
		StudentVo vo = new StudentVo();
		BeanUtils.copyProperties(student, vo);
		Integer cid_1 = student.getClub1Id();
		if (cid_1 != null) {
			Club club1 = clubService.getById(cid_1);
			vo.setClub1(club1.getName());
		} else {
			vo.setClub1(null);
		}
		Integer cid_2 = student.getClub2Id();
		if (cid_2 != null) {
			Club club2 = clubService.getById(cid_2);
			vo.setClub2(club2.getName());
		} else {
			vo.setClub2(null);
		}
		if (student.getSex() == 1) {
			vo.setSex("男");
		} else if (student.getSex() == 0) {
			vo.setSex("女");
		}
		return vo;
	}

	@Override
	public int getCountByClubId(int cid) {
		QueryWrapper<Student> wrapper = new QueryWrapper<>();
		wrapper.eq("club1_id", cid).or().eq("club2_id", cid);
		return this.count(wrapper);
	}

	@Override
	public List<StudentInfo> ListSignalClubStudentInfo() {
		QueryWrapper<Student> wrapper = new QueryWrapper<>();
		wrapper.isNotNull("club1_id").or().isNotNull("club2_id");
		List<Student> oldList = this.list(wrapper);
		List<StudentInfo> list = new ArrayList<>(2 * oldList.size());
		oldList.forEach((student) -> {
			String id = student.getId();
			String name = student.getName();
			String department = student.getDepartment();
			Integer club1Id = student.getClub1Id();
			Integer club2Id = student.getClub2Id();
			int flag = 0;
			if (club1Id != null && club2Id != null) {
				flag = 3;
			} else if (club1Id == null && club2Id == null) {
				flag = 0;
			} else if (club1Id == null) {
				flag = 2;
			} else {
				flag = 1;
			}
			switch (flag) {
				case 1:
					list.add(new StudentInfo(id, name, department, club1Id));
					break;
				case 2:
					list.add(new StudentInfo(id, name, department, club2Id));
					break;
				case 3:
					list.add(new StudentInfo(id, name, department, club1Id));
					list.add(new StudentInfo(id, name, department, club2Id));
					break;
				default:
					// nothing
			}
		});
		return list;
	}

}
